﻿using Elio.Controls;
using Elio.Interfaces;
using Elio.Sessions;
using Elio.Sessions.Widgets;
using Google.Protobuf.WellKnownTypes;

namespace Elio.Widgets;

public class WidgetImage : Image, ISessionWidget
{
    public BaseSession? Session { get; set; }
    public void Init(InitOptions options)
    {
        (WidthRequest, HeightRequest) = (options.Width, options.Height);
        this.SetPoint(options.X, options.Y);
    }

    public async Task<bool> OnViewReceive(string name, long sid, long fid, params Any[] argv)
    {
        switch (name)
        {
            case "getSrc":
                await GetSrc(sid, fid);
                break;
            case "setSrc":
                await SetSrc(argv);
                break;
            default:
                return false;
        }
        return true;
    }

    private async Task GetSrc(long sid, long fid) =>
        await Session!.SendAction("onGetSrc", sid, fid, _src.ToAny());

    private Task SetSrc(params Any[] argv) =>
        Task.FromResult(_src = argv[0].P2String());

    private string _src = string.Empty;
}
